package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailStockMsg;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.ReqRetailStockMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.RetAtsRetailStockMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 推送经销商零售与库存信息
 *
 * @author Parker on 2019-11-19.
 * @version 1.0
 */
public class CIB02RetailStockMsg extends CIB02Base implements IRetailStockMsg {
    @Override
    public RetAtsRetailStockMsg getRetailStockMsg(ReqRetailStockMsgQry qry) throws Exception {
        RetAtsRetailStockMsg retAts = new RetAtsRetailStockMsg(qry.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "28005");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("YURREF", qry.reqHeader.transSeqID);//业务参考号
            xmlWriter.writeElementString("REV50L", "");//保留字段
            while (qry.readRecord()) {
                xmlWriter.writeStartElement("DATA");
                xmlWriter.writeElementString("DLENUM", qry.ReqDetail.DealerCode);//经销商代码
                xmlWriter.writeElementString("DLENAM", qry.ReqDetail.DealerName);//经销商名称
                if(getBankConfig().getValueAsBool("pushYearData")){//是否推送年度累计零售数据
                    xmlWriter.writeElementString("YCRTOL", qry.ReqDetail.Ndljls);//年度累计零售车辆总数
                    xmlWriter.writeElementString("YCRAMT", qry.ReqDetail.NdljlsMoney);//年度累计零售车辆总金额
                }else{
                    xmlWriter.writeElementString("YCRTOL", "");//年度累计零售车辆总数
                    xmlWriter.writeElementString("YCRAMT", "");//年度累计零售车辆总金额
                }
                xmlWriter.writeElementString("MCRTOL", qry.ReqDetail.Byljls);//本月累计零售车辆总数
                xmlWriter.writeElementString("MCRAMT", qry.ReqDetail.ByljlsMoney);//本月累计零售车辆总金额
                xmlWriter.writeElementString("DCRTOL", qry.ReqDetail.Drlscl);//当日零售车辆总数
                xmlWriter.writeElementString("DCRAMT", qry.ReqDetail.DrslclMoney);//当日零售车辆总金额
                xmlWriter.writeElementString("TCRTOL", qry.ReqDetail.Ssckcl);//实时库存车辆总数
                xmlWriter.writeElementString("TCRAMT", qry.ReqDetail.SsckclMoney);//实时库存车辆总金额
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), "T28005");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if ("SCDS000".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/RESPONSE/DATA")) {
                            String itemRetCode = tmpBkReader.readedXElement.getSingleTextNotNull("ERRCOD");
                            String itemRetMsg = tmpBkReader.readedXElement.getSingleTextNotNull("ERRMSG");
                            if (itemRetCode.equals("SCDS000")) {
                                tmpRespCode = "0";
                                tmpRespInfo = "处理成功";
                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = itemRetCode + " : " + itemRetMsg;
                                WriteBankLogLn("失败原因：" + tmpRespInfo);
                                break;
                            }
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetCode + " : " + tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
